﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;

namespace ET.DBCommon.DAL
{
    public class OperationLogDAL
    {
        public static bool InsertOperationLog(OperationLogModel operationLog)
        {
            string sql = @"INSERT INTO operationlog
                            (`UserId`,
                            `OperateId`,
                            `OperatedTime`,
                            `VirtualTime`,
                            `DeviceTypeId`,
                            `DeviceId`,
                            `Parameters`,
                            `Remark`,
                            `SysType`,
                            `AudioFile`,
                            `ExamId`,
                            `RoleId`)
                            VALUES
                            (@UserId,
                            @OperateId,
                            now(6),
                            @VirtualTime,
                            @DeviceTypeID,
                            @DeviceId,
                            @Parameters,
                            @Remark,
                            @SysType,
                            @AudioFile,
                            @ExamId
                            ,@RoleId)";
            MySqlParameter[] parms = {
                new MySqlParameter("@UserId", operationLog.UserId ),
                new MySqlParameter("@OperateId", operationLog.OperateId),
                //new MySqlParameter("@OperTime", operationLog.OperTime),
                new MySqlParameter("@VirtualTime", operationLog.VirtualTime),

                new MySqlParameter("@DeviceTypeId", operationLog.DeviceTypeId ),
                new MySqlParameter("@DeviceId", operationLog.DeviceId),
                new MySqlParameter("@Parameters", string.IsNullOrWhiteSpace(operationLog.Parameters)?"":operationLog.Parameters),
                new MySqlParameter("@Remark", string.IsNullOrWhiteSpace(operationLog.Remark)?"":operationLog.Remark),
                new MySqlParameter("@SysType", operationLog.SysType ),
                new MySqlParameter("@AudioFile", operationLog.AudioFile),
                new MySqlParameter("@ExamId", operationLog.ExamId),
                new MySqlParameter("@RoleId", operationLog.RoleId)
            };
            return DbHelperLog.ExecuteNonQuery(sql, parms) > 0;
        }

        public static DataSet SelectOperationLog(int userId, DateTime start, DateTime end)
        {
            string sql = "SELECT a.*,b.`Describe` FROM operationlog a inner join stepitem b on a.stepid=b.stepid" +
                " where a.useroperationtime between @Start and @End and a.UserID=@UserID";

            MySqlParameter[] parms = {
                new MySqlParameter("@UserID", userId),
                new MySqlParameter("@Start", start ),
                new MySqlParameter("@End", end)
            };
            return DbHelperLog.ExecuteDataset(CommandType.Text, sql, parms);
        }

        public static void UpdateParameters(int Id, string parameters)
        {
            string sql = "update operationlog set Parameters = @Parameters where Id=@Id";

            MySqlParameter[] parms = {
                new MySqlParameter("@Parameters", parameters),
                new MySqlParameter("@Id", Id )
            };
            DbHelperLog.ExecuteNonQuery(CommandType.Text, sql, parms);
        }

        public static void SeclectOperationOrderNum(int id,out List<short> Numlist)
        {
            Numlist = new List<short>();
            string sql = "SELECT * FROM operationlog where OperateId=59 And ExamId=" + id;
            DataSet ds = DbHelper.ExecuteDataset(sql);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][7] != null)
                    {
                        Numlist.Add(Convert.ToInt16(dt.Rows[i][7]));
                    }
                    
                }
            }
                   
        }
        public static void SeclectOperationOrderNum1(int id, out List<short> Numlist)
        {
            Numlist = new List<short>();
            string sql = "SELECT * FROM operationlog where OperateId=60 And ExamId=" + id;
            DataSet ds = DbHelper.ExecuteDataset(sql);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][7] != null)
                    {
                        Numlist.Add(Convert.ToInt16(dt.Rows[i][7]));
                    }

                }
            }

        }
        public static void SelectExamId(out int id)
        {
            string sql =" select * from operationlog order by id desc limit 1";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            DataTable dt = ds.Tables[0];
            id = (int)dt.Rows[0][11];
        }


    }
}
